<?php
//计算总数
function zong_shu($table,$where = null){
    $count = M($table)->where($where)->count();
    if(!$count){$count = 0;}
    return $count;
}
//把一维数组转成二维数组
function zhuan_arr($biao){
    $height = array();
    for ($i=0;$i<count($biao);$i++){
        $a['name'] = $biao[$i];
        $a['id'] = $i;
        array_push($height,$a);
    }
    return $height;
}
/**
 * 系统邮件发送函数
 * @param string $to    接收邮件者邮箱
 * @param string $name  接收邮件者名称
 * @param string $subject 邮件主题
 * @param string $body    邮件内容
 * @param string $attachment 附件列表
 * @return boolean
 */
//所用邮箱需开启 POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务   在邮箱-》账户  获取开启码
require 'ThinkPHP/Extend/Vendor/PHPMailer/class.phpmailer.php';
require 'ThinkPHP/Extend/Vendor/PHPMailer/class.smtp.php';
function think_send_mail($to, $name, $subject = '', $body = '', $attachment = null){
    $config = C('THINK_EMAIL');
    vendor('PHPMailer.class#phpmailer'); //从PHPMailer目录导class.phpmailer.php类文件
    $mail              = new PHPMailer(); //PHPMailer对象
    $mail->CharSet    = 'UTF-8'; //设定邮件编码，默认ISO-8859-1，如果发中文此项必须设置，否则乱码
    $mail->IsSMTP();  // 设定使用SMTP服务
    $mail->SMTPDebug  = 0;                     // 关闭SMTP调试功能
    // 1 = errors and messages
    // 2 = messages only
    $mail->SMTPAuth   = true;                  // 启用 SMTP 验证功能
    $mail->SMTPSecure = 'ssl';                 // 使用安全协议
    $mail->Host       = $config['SMTP_HOST'];  // SMTP 服务器
    $mail->Port       = $config['SMTP_PORT'];  // SMTP服务器的端口号
    $mail->Username   = $config['SMTP_USER'];  // SMTP服务器用户名
    $mail->Password   = $config['SMTP_PASS'];  // SMTP服务器密码
    $mail->SetFrom($config['FROM_EMAIL'], $config['FROM_NAME']);
    $replyEmail       = $config['REPLY_EMAIL']?$config['REPLY_EMAIL']:$config['FROM_EMAIL'];
    $replyName        = $config['REPLY_NAME']?$config['REPLY_NAME']:$config['FROM_NAME'];
    $mail->AddReplyTo($replyEmail, $replyName);
    $mail->Subject    = $subject;
    $mail->MsgHTML($body);
    $mail->AddAddress($to, $name);
    if(is_array($attachment)){ // 添加附件
        foreach ($attachment as $file){
            is_file($file) && $mail->AddAttachment($file);
        }
    }
    //return $mail->Send() ? true : $mail->ErrorInfo;
    return $mail->Send() ? true : false;
}
//出生日期计算年龄  $s为时间戳
function ages($s){
    $birthday = date("Y-m-d",$s);
    $age = date('Y', time()) - date('Y', strtotime($birthday)) - 1;
    if (date('m', time()) == date('m', strtotime($birthday))){

        if (date('d', time()) > date('d', strtotime($birthday))){
            $age++;
        }
    }elseif (date('m', time()) > date('m', strtotime($birthday))){
        $age++;
    }
    if($age < 1){
        $age = 1;
    }
    return $age;
}

//微信模板消息
// $template_id  模板id
//$page   跳转页面
//$ids   数组/单个 （发送者的id）
function sends_muban($template_id,$page,$qiu_id,$fa_id){  //发送模板消息 $qiu_id（接收用户的id） $fa_id(发送用户的id)

    //当PHP运行在安全模式下时此函数无效。除了关闭安全模式或者在php.ini程序中修改最大运行时间没有其他办法让此函数运行
    /* ignore_user_abort();//关闭浏览器仍然执行
     set_time_limit(0);//让程序一直执行下去
     $interval=10*1;//每隔一定时间运行
     do{*/
    $time    = date("Y-m-d H:i:s",time());
    if(is_array($qiu_id)){
        $ids = implode(",",$qiu_id);
        $sel = joins("form_id","a.*,b.nick_name,b.sex,c.openid",array("a left join __USERS_JIINFO__ b on a.user_id = b.user_id","left join __USERS__ c on a.user_id = c.id"),array("a.user_id"=>array("in",$ids)),"id asc");
    }else{
        $sel = joins("form_id","a.*,b.nick_name,b.sex,c.openid",array("a left join __USERS_JIINFO__ b on a.user_id = b.user_id","left join __USERS__ c on a.user_id = c.id"),"a.user_id = {$qiu_id}");
    }
    if($sel){
        foreach ($sel as $k=>$v){
            $a            = explode(",",$v['form_id']);
            $touser      = $v['openid'];
            //$template_id = "SRWUhHzEU_JRQelDL14i-HVMAB-AEoJkXxpzUV_HE6w";
            //$page        = "pages/index/index";
            $fa_u = sel("users_jiinfo","nick_name,sex","user_id = {$fa_id}");
            $nickname    = $fa_u['nick_name'];
            if($fa_u['sex'] == 1){$sex = "男";}
            if($fa_u['sex'] == 2){$sex = "女";}
            $form_id     = $a[0];
            unset($a[0]);
            $b            = implode(",",$a);
            $cun           = up_one("form_id","id = {$v['id']}",array("form_id"=>$b,"up_at"=>time()));

            $arr         = array("touser"=>$touser,"template_id"=>$template_id,"page"=>$page,"form_id"=>$form_id,"data"=>array("keyword1"=>array("value"=>"小投票通知测试","color"=>"#173177"),"keyword1"=>array("value"=>$nickname,"color"=>"#173177"),"keyword2"=>array("value"=>$sex,"color"=>"#173177"),"keyword3"=>array("value"=>"申请交换您的QQ/微信账号","color"=>"#173177")));
            $data        = json_encode($arr);

            $access_token = getAccessToken();
            $url          = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token={$access_token}";
            $a = httpPost($url,$data);
        }
    }
}
function httpGet($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_URL, $url);

    $res = curl_exec($curl);
    curl_close($curl);

    return $res;
}
function httpPost($url,$data){

    $ch         = curl_init();
    $c_url      = $url;
    $c_url_data = $data;

    curl_setopt($ch, CURLOPT_URL,$c_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //设置header
    //header('Content-Type: image/jpeg');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $c_url_data);

    $result     = curl_exec($ch);
    curl_close ($ch);
    return $result;
}